package com.gg.utils

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import java.util.regex.Pattern
import android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE
import android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
import android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
import android.os.Build.VERSION
import android.view.View


/**
 *  Creator : GG
 *  Time    : 2017/10/16
 *  Mail    : gg.jin.yu@gmail.com
 *  Explain : The `fragment` is added to the container view with id `frameId`. The operation is
 *            performed by the `fragmentManager`.
 */
fun AppCompatActivity.replaceFragmentInActivity(fragment: Fragment, @IdRes frameId: Int) {
    supportFragmentManager.transact {
        replace(frameId, fragment)
    }
}

/**
 * The `fragment` is added to the container view with tag. The operation is
 * performed by the `fragmentManager`.
 */
fun AppCompatActivity.addFragmentToActivity(fragment: Fragment, tag: String) {
    supportFragmentManager.transact {
        add(fragment, tag)
    }
}

fun AppCompatActivity.setupActionBar(@IdRes toolbarId: Int, action: ActionBar.() -> Unit) {
    setSupportActionBar(findViewById(toolbarId))
    supportActionBar?.run {
        action()
    }
}

fun Activity.setTransparent() {
    if (Build.VERSION.SDK_INT >= 21) {
        val decorView = window.decorView
        val option = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        decorView.systemUiVisibility = option
        window.navigationBarColor = Color.TRANSPARENT
        window.statusBarColor = Color.TRANSPARENT
    }
}

fun FragmentTransaction.addAnimation() {
    setCustomAnimations(
            R.anim.slide_right_in,
            R.anim.slide_left_out,
            R.anim.slide_left_in,
            R.anim.slide_right_out
    )
}

/**
 * Runs a FragmentTransaction, then calls commit().
 */
private inline fun FragmentManager.transact(action: FragmentTransaction.() -> Unit) {
    beginTransaction().apply {
        action()
    }.commit()
}


fun Context.toast(str: String, length: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this.applicationContext, str, length).show()
}

fun Context.toast(int: Int, length: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this.applicationContext, int, length).show()
}

fun AppCompatActivity.context(): Context = this

fun Fragment.toast(str: String, length: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(context?.applicationContext, str, length).show()
}

fun Fragment.toast(int: Int, length: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(context?.applicationContext, int, length).show()
}


fun String.findCarIdByUrl(param: String): String {
    val mc = Pattern.compile("(^|\\?|&)$param=([^&|#]*)(&|$|#)").matcher(this)
    return if (mc.find()) {
        mc.group(2)
    } else ""
}

fun String.isPhoneNum(): Boolean {
    val p = Pattern.compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(147))\\d{8}$")
    val m = p.matcher(this)
    return m.matches()
}

fun Int?.isNullOrZero(): Boolean {
    return this == null || this == 0
}

fun Double?.isNullOrZero(): Boolean {
    return this == null || this == 0.0
}

fun Long?.isNullOrZero(): Boolean {
    return this == null || this == 0L

}

fun CharSequence.vinSpot(): String {
    return toString().vinSpot()
}

fun String.vinSpot(): String {
    return replaceSpecialStr()
            .toUpperCase()
            .replace("I", "1")
            .replace("O", "0")
            .replace("Q", "0")
}

/**
 * 去除字符串中的空格、回车、换行符、制表符等
 * @param str
 * @return
 */
fun String.replaceSpecialStr(): String {
    val p = Pattern.compile("\\s*|\t|\r|\n")
    val m = p.matcher(this)
    return m.replaceAll("")
}
